/**
* 基础菜单 品类管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>品类管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" class="user-search">
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEdit()"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="$store.state.loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="id" label="id" width="200">
      </el-table-column>
      <el-table-column sortable prop="name" label="名称" width="150">
      </el-table-column>
      <el-table-column sortable prop="createAt" label="添加时间" width="150">
        <template slot-scope="scope">
          <div>{{ scope.row.createAt | timestampToTime }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="100">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-popconfirm
            title="确认要删除？"
            @confirm="deleteCategory(scope.$index, scope.row)"
            class="mg-left-10"
          >
            <el-button slot="reference" size="mini" plain type="danger"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="40%"
      @click="closeDialog"
    >
      <el-form label-width="120px" :model="editForm" ref="editForm">
        <el-form-item label="id" prop="id">
          <el-input
            size="small"
            v-model="editForm.id"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input
            size="small"
            v-model="editForm.name"
            auto-complete="off"
            placeholder="请输入品类名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="$store.state.loading"
          class="title"
          @click="submitEditForm('editForm')"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { CategoryList, CategorySave, CategoryDel } from "../../api/productApi";

export default {
  data() {
    return {
      loading: false, //是显示加载
      editFormVisible: false,
      title: "添加",
      editForm: {
        id: "",
        name: "",
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
    };
  },
  // 注册组件
  components: {},
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getdata();
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    getdata(parameter) {
      CategoryList(parameter).then((res) => {
        this.listData = res;
      });
    },
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        this.title = "编辑品类";
        this.editForm.id = row.id;
        this.editForm.name = row.name;
      } else {
        this.title = "添加品类";
        this.editForm.id = "";
        this.editForm.name = "";
      }
    },
    deleteCategory: function (index, row) {
      CategoryDel(row).then(() => {
        this.$message({
          type: "success",
          message: "删除成功",
        });
        this.listData.splice(index, 1);
      });
    },
    submitEditForm(editData) {
      this.$refs[editData].validate((valid) => {
        if (valid) {
          CategorySave(this.editForm).then(
            () => {
              this.editFormVisible = false;
              this.$message({
                type: "success",
                message: "保存成功！",
              });
              this.getdata();
            },
            () => {
              this.editFormVisible = false;
            }
          );
        }
      });
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}

.userRole {
  width: 100%;
}
</style>
